const {
	parentPort,
	isMainThread
} = require("worker_threads")
const fs = require('fs')
const path = require('path')


parentPort.on("message", (res) => {
	let {
		fileFoldPath,
		filePath,
		file,
		index
	} = res[0]

	// 重定文件路径
	fileFoldPath = path.join(fileFoldPath, '..', "newJson", file)
	// 读取文件
	fs.readdir(fileFoldPath, (err, have) => {
		if (!have) {
			// 读取失败则重新创建
			fs.mkdirSync(fileFoldPath, 0777, true)
		}

		let newFilePath = path.join(fileFoldPath, "new.txt")
		let fileData = fs.readFileSync(filePath)

		fs.writeFileSync(newFilePath, fileData)
		parentPort.postMessage([index])
		parentPort.close()
	})
})
